
package com.companymodule.dao;
import com.companymodule.bean.TbCompanyProductMiddle;
import java.sql.*;
import java.io.*;
import java.util.*;
import util.*;
public class TbCompanyProductMiddleDAO {
    private Connection conn = null;
    private PreparedStatement pt = null;
    private ResultSet rs = null;
    private boolean flag = false;
    private String sql = "";
    private TbCompanyProductMiddle tbCompanyProductMiddle=null;
    private List<TbCompanyProductMiddle> tbCompanyProductMiddleList=null;

    public boolean add(TbCompanyProductMiddle inTbCompanyProductMiddle){
        try{
            
        conn=ConnectDB.getSqlServerConnection();
        sql="insert into Tb_CompanyProductMiddle(CompanyId,ProductId) values(?,?)";
        pt = conn.prepareStatement(sql);
        
        pt.setInt(1,inTbCompanyProductMiddle.getCompanyId());
        pt.setInt(2,inTbCompanyProductMiddle.getProductId());
        if (pt.executeUpdate() > 0) flag=true;
        }catch(Exception e){
            e.printStackTrace();
        }finally {
                        ConnectDB.closeSqlConnection();        }
        return flag;
    }
    public boolean del(int inCompanyProductMiddleId){
        return UtilDAO.del("Tb_CompanyProductMiddle", "CompanyProductMiddleId", inCompanyProductMiddleId);
    }
    public boolean edit(TbCompanyProductMiddle inTbCompanyProductMiddle){
        try{
            
        conn=ConnectDB.getSqlServerConnection();
        sql="update Tb_CompanyProductMiddle set CompanyProductMiddleId=?,CompanyId=?,ProductId=? where CompanyProductMiddleId=?";
        pt = conn.prepareStatement(sql);
        
        pt.setInt(1,inTbCompanyProductMiddle.getCompanyProductMiddleId());
        pt.setInt(2,inTbCompanyProductMiddle.getCompanyId());
        pt.setInt(3,inTbCompanyProductMiddle.getProductId());
        pt.setInt(4,inTbCompanyProductMiddle.getCompanyProductMiddleId());
        if (pt.executeUpdate() > 0) flag=true;
        }catch(Exception e){
            e.printStackTrace();
        }finally {
                        ConnectDB.closeSqlConnection();        }
        return flag;
    }
    public TbCompanyProductMiddle getTbCompanyProductMiddleByCompanyProductMiddleId(int inCompanyProductMiddleId) {
        tbCompanyProductMiddle=null;
        try{
            conn = ConnectDB.getSqlServerConnection();
            sql = "select * from Tb_CompanyProductMiddle where CompanyProductMiddleId=? ";
            pt = conn.prepareStatement(sql);
            pt.setInt(1,inCompanyProductMiddleId);
            rs = pt.executeQuery();
            while (rs.next()) {
                if (tbCompanyProductMiddle == null)tbCompanyProductMiddle = getTbCompanyProductMiddle(rs);
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
                        ConnectDB.closeSqlConnection();        }
        return tbCompanyProductMiddle;
    }
    public List<TbCompanyProductMiddle> getTbCompanyProductMiddleAll() {
        tbCompanyProductMiddleList=new ArrayList<TbCompanyProductMiddle>();
        try{
            conn = ConnectDB.getSqlServerConnection();
            sql = "select * from Tb_CompanyProductMiddle";
            pt = conn.prepareStatement(sql);
            rs = pt.executeQuery();
            while (rs.next()) {
                tbCompanyProductMiddleList.add(getTbCompanyProductMiddle(rs));
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
                        ConnectDB.closeSqlConnection();        }
        return tbCompanyProductMiddleList;
    }
    public List<TbCompanyProductMiddle> getTbCompanyProductMiddleAll(VskPage vp) {
        tbCompanyProductMiddleList=new ArrayList<TbCompanyProductMiddle>();
        try{
            conn = ConnectDB.getSqlServerConnection();
            sql = "select * from Tb_CompanyProductMiddle";
            vp.setAllCountSql(sql);
            vp.getAllRecordCount(conn, vp.getAllCountSql());
            pt = conn.prepareStatement(vp.processPageSql( "Tb_CompanyProductMiddle", "CompanyProductMiddleId"));
            rs = pt.executeQuery();
            while (rs.next()) {
                tbCompanyProductMiddleList.add(getTbCompanyProductMiddle(rs));
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
                        ConnectDB.closeSqlConnection();        }
        return tbCompanyProductMiddleList;
    }
    public TbCompanyProductMiddle getTbCompanyProductMiddle(ResultSet rs) throws Exception{
        TbCompanyProductMiddle tbCompanyProductMiddle = new TbCompanyProductMiddle();
        tbCompanyProductMiddle.setCompanyProductMiddleId(rs.getInt("CompanyProductMiddleId"));
        tbCompanyProductMiddle.setCompanyId(rs.getInt("CompanyId"));
        tbCompanyProductMiddle.setProductId(rs.getInt("ProductId"));
        return tbCompanyProductMiddle;
    }
}
